package com.ruoyi.common.tools.excel.listener;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;

import com.alibaba.excel.context.AnalysisContext;
import com.ruoyi.common.core.service.DeptService;
import com.ruoyi.common.core.service.DictService;
import com.ruoyi.common.tools.constant.ERecordConstant;
import com.ruoyi.common.tools.excel.base.MyExcelListener;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.erecord.employee.service.IEmployeeService;
import com.ruoyi.erecord.vehicles.domain.vo.VehiclesImportDetailVo;


/**
 * 车辆 excel 导入
 * @author cc
 *
 */
public class VehiclesExcelListener extends MyExcelListener<VehiclesImportDetailVo> {
	
	private DeptService deptService = SpringUtils.getBean(DeptService.class);
	private DictService dictService = SpringUtils.getBean(DictService.class);
	private IEmployeeService employeeService = SpringUtils.getBean(IEmployeeService.class);
	
	private String batchNo = null;
    public VehiclesExcelListener(String batchNo) {
    	this.batchNo = batchNo;
    }
    
    @Override
    public void doValidate(VehiclesImportDetailVo data, AnalysisContext context) {
    	//1.设置批次号
    	//2.校验数据 与 匹配数据字典数据 设置对应字段
    	//3.设置 处理状态 与 处理信息
    	List<String> validateList = new ArrayList<>();
    	
    	data.setBatchNo(this.batchNo);
    	
    	if(StringUtils.isEmpty(data.getVehiclesCode())) {
    		validateList.add("[ 车辆编码 ] 为空");
    	}
    	
    	if(StringUtils.isEmpty(data.getVehiclesName())) {
    		validateList.add("[ 车辆设备名称 ] 为空");
    	}
    	
    	if(StringUtils.isEmpty(data.getVehiclesType())) {
    		validateList.add("[ 车辆类型 ] 为空");
    	}
    	
    	if(StringUtils.isEmpty(data.getVehiclesNumber())) {
    		validateList.add("[ 车牌号码 ] 为空");
    	}
    	
    	if (!StringUtils.isEmpty(data.getDeptIdImport())) {
    		long deptId = NumberUtils.toLong(this.deptService.selectDeptIdByName(data.getDeptIdImport()) , -1);
    		if (deptId == -1) {
    			validateList.add("[ 服务部门 ] 匹配不到数据");
    		} else {
    			data.setDeptId(deptId);
    		}
    	} else {
    		validateList.add("[ 服务部门 ] 为空");
    	}
     
    	if (!StringUtils.isEmpty(data.getServiceTypeImport())) {
    		int val = NumberUtils.toInt(this.dictService.getDictValue("erecord_service_type", data.getServiceTypeImport()) , -1); 
    		if (val == -1) {
    			validateList.add("[ 服务类型 ] 匹配不到数据");
    		} else {
    			data.setServiceType(val);
    		}
    	} 
    	
    	if (!StringUtils.isEmpty(data.getEmployeeCodeImport())) {
    		Long val = employeeService.getEmployeeIdByName(data.getEmployeeCodeImport()); 
    		if (null == val) {
    			validateList.add("[ 驾驶员 ] 匹配不到数据");
    		} else {
    			data.setEmployeeCode(val);
    		}
    	}
    	
    	if (!StringUtils.isEmpty(data.getEvFlagImport())) {
    		String val = this.dictService.getDictValue("sys_yes_no", data.getEvFlagImport()); 
    		if (StringUtils.isEmpty(val)) {
    			validateList.add("[ 纯电动 ] 匹配不到数据");
    		} else {
    			data.setEvFlag(val);
    		}
    	}
    	
    	if (!StringUtils.isEmpty(data.getGpsFlagImprort())) {
    		String val = this.dictService.getDictValue("sys_yes_no", data.getGpsFlagImprort()) ; 
    		if (StringUtils.isEmpty(val)) {
    			validateList.add("[ 是否加装GPS ] 匹配不到数据");
    		} else {
    			data.setGpsFlag(val);
    		}
    	}
    	
    	if (!StringUtils.isEmpty(data.getDrivingVehiclesType())) {
    		String val =this.dictService.getDictValue("erecord_driving_vehicles_type", data.getDrivingVehiclesType()); 
    		if (StringUtils.isEmpty(val)) {
    			validateList.add("[ 准驾车型 ] 匹配不到数据");
    		}
    	}
    	
    	
    	if (!validateList.isEmpty()) {
    		String errMsg = String.join(" ，", validateList);
    		excelResult.getValidateList().add(errMsg);
    		
    		data.setProcessMsg(errMsg);
    		data.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
    	} else {
    		data.setProcessMsg("成功");
    		data.setProcessStatus(ERecordConstant.COMMON_STATUS_SUCCESS);
    	}
    }

	@Override
	protected void doOnExceptionProcess(String errorMsg) {
		VehiclesImportDetailVo vo = new VehiclesImportDetailVo();
		vo.setBatchNo(this.batchNo);
		vo.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
		vo.setProcessMsg(errorMsg);
		this.excelResult.getList().add(vo);
	}
}
